<?php

namespace app\models;

use yii;
use yii\db\Query;

class Repo extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'repo_fee';
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['pid'])) $db->andWhere(['pid' => $where['pid']]);

        if (!empty($where['invoice_order'])) {
            if (is_array($where['invoice_order'])) {
                $db->andWhere(['in', 'invoice_order', $where['invoice_order']]);
            } else {
                $db->andWhere(['invoice_order' => $where['invoice_order']]);
            }
        }

        return $db->all();
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }
        if (!empty($where['pid'])) {
            $db->andWhere(['pid' => $where['pid']]);
        }
        return $db->limit(1)->one();
    }


    public function get_repo_list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);

        if ($where['name']) {
            $db->andWhere(['name' => $where['name']]);
        }
        if ($where['invoice_order']) {
            $db->andWhere(['invoice_order' => $where['invoice_order']]);
        }
        if ($where['num']) {
            $db->andWhere(['num' => $where['num']]);
        }
        if ($where['begin_date']) {
            $db->andWhere(['>=', 'date', $where['begin_date']]);
        }
        if ($where['end_date']) {
            $db->andWhere(['<=', 'date', $where['end_date']]);
        }
        if ($where['customer_name']) {
            $db->andWhere(['like', 'customer_name', $where['customer_name']]);
        }
        if ($where['product']) {
            $db->andWhere(['like', 'product', $where['product']]);
        }

        $db->orderBy(['date' => SORT_DESC, 'pid' => SORT_DESC,'id'=>SORT_ASC])
            ->offset(($p - 1) * $s)
            ->limit($s);
        $list = $db->all();

        return $list;
    }

    public function get_repo_size($where = [])
    {
        $db = new Query();
        $db->select('count(id) as num');
        if ($where['name']) {
            $db->andWhere(['name' => $where['name']]);
        }

        if ($where['begin_date']) {
            $db->andWhere(['>=', 'date', $where['begin_date']]);
        }

        if ($where['end_date']) {
            $db->andWhere(['<=', 'date', $where['end_date']]);
        }

        if ($where['num']) {
            $db->andWhere(['num' => $where['num']]);
        }

        if ($where['customer_name']) {
            $db->andWhere(['like', 'customer_name', $where['customer_name']]);
        }

        if ($where['invoice_order']) {
            $db->andWhere(['invoice_order' => $where['invoice_order']]);
        }
        if ($where['product']) {
            $db->andWhere(['like', 'product', $where['product']]);
        }
        $info = $db->from($this->table)->one();
        if ($info) {
            return $info['num'];
        }

        return 0;
    }


    //查询invoice费用
    public function get_repo_fee_by_invoice($invoice_arr)
    {
        $query = new Query();
        $query->select('invoice_order,pay_all,num')
            ->from($this->table)
            ->where(['in', 'invoice_order', $invoice_arr]);

        $db = new Query();
        $list = $db->select('sum(pay_all) as pay_all,invoice_order,num')
            ->from(['a' => $query])
            ->groupBy('invoice_order')
            ->all();

        return $list;
    }

    //获取单条repo费用
    public function get_repo_info_by_invoice($invoice)
    {
        $db = new Query();
        return $db->select('*')->from($this->table)->where(['invoice_order' => $invoice])->all();
    }

}
